package org.duiduo.common.runtime.mq.topic;

import org.duiduo.common.bean.constant.RabbitmqConsts;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * Topic配置
 *
 * @author liangze
 * @date 2023/06/04
 */
@Order(1)
@Configuration
@ConditionalOnProperty(name = "rabbitmq.enable", havingValue = "true")
public class TopicConfig {

    /**
     * 交换机
     *
     * @return {@link TopicExchange}
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(RabbitmqConsts.TOPIC_EXCHANGE, Boolean.TRUE, Boolean.FALSE);
    }

    /**
     * 抽奖兑现队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue luckdrawCashQueue() {
        return new Queue(RabbitmqConsts.LUCKDRAW_CASH_QUEUE, Boolean.TRUE);
    }

    /**
     * 抽奖兑现绑定
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding luckdrawCashBinding() {
        return BindingBuilder.bind(luckdrawCashQueue())
                .to(topicExchange())
                .with(RabbitmqConsts.LUCKDRAW_CASH_ROUTING_KEY);
    }

    /**
     * 短信发送队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue sendCodeQueue() {
        return new Queue(RabbitmqConsts.SEND_CODE_QUEUE, Boolean.TRUE);
    }

    /**
     * 短信发送绑定
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding sendCodeBinding() {
        return BindingBuilder.bind(sendCodeQueue())
                .to(topicExchange())
                .with(RabbitmqConsts.SEND_CODE_ROUTING_KEY);
    }

    /**
     * 邮件发送队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue sendMailQueue() {
        return new Queue(RabbitmqConsts.SEND_MAIL_QUEUE, Boolean.TRUE);
    }

    /**
     * 邮件发送绑定
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding sendMailBinding() {
        return BindingBuilder.bind(sendMailQueue())
                .to(topicExchange())
                .with(RabbitmqConsts.SEND_MAIL_ROUTING_KEY);
    }

    /**
     * 告警发送队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue sendAlarmQueue() {
        return new Queue(RabbitmqConsts.SEND_ALARM_QUEUE, Boolean.TRUE);
    }

    /**
     * 告警发送绑定
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding sendAlarmBinding() {
        return BindingBuilder.bind(sendAlarmQueue())
                .to(topicExchange())
                .with(RabbitmqConsts.SEND_ALARM_ROUTING_KEY);
    }

    /**
     * 告警发送队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue callXrgPermissionQueue() {
        return new Queue(RabbitmqConsts.XRG_PERMISSION_CALL_QUEUE, Boolean.TRUE);
    }

    /**
     * 告警发送绑定
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding callXrgPermissionBinding() {
        return BindingBuilder.bind(callXrgPermissionQueue())
                .to(topicExchange())
                .with(RabbitmqConsts.XRG_PERMISSION_CALL_ROUTING_KEY);
    }

}
